package com.huiyujinrong.qiangdanbao.activity.my;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.adapter.RecordAdapter;
import com.huiyujinrong.qiangdanbao.bean.RecordBean;
import com.huiyujinrong.qiangdanbao.network.Api;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 交易记录
 */
public class RecordActivity extends AppCompatActivity implements RecordContract.View {


    @BindView(R.id.toolbar_Title)
    TextView title;
    @BindView(R.id.toolbar_Back)
    RelativeLayout back;

    @BindView(R.id.rc_listview)
    ListView rc_listview;
    @BindView(R.id.rc_pull)
    PullRefreshLayout rc_pull;

    private RecordBean rc_bean;

    private List<RecordBean.list> beanList;

    private RecordAdapter adapter;

    private RecordPresenter presenter;

    private Toast t;

    private int pullNum = 1;
    private boolean isReflush;
    @BindView(R.id.tvMessage)
    TextView tvMessage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        ButterKnife.bind(this);

        inSetToolBar();

        presenter = new RecordPresenter(this, this);
        presenter.start();


        isReflush = true;
        rc_pull.setRefreshing(true);
        presenter.getRecordData(String.valueOf(SharedHelper.get(RecordActivity.this, Api.USER_ID, Api.nul)), pullNum);

        beanList = new ArrayList<>();
        adapter = new RecordAdapter(beanList, this);
        rc_listview.setAdapter(adapter);

        //刷新
        rc_pull.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                rc_pull.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isReflush = true;
                        pullNum = 1;
                        presenter.getRecordData(String.valueOf(SharedHelper.get(RecordActivity.this, Api.USER_ID, Api.nul)), pullNum);
                        rc_pull.setRefreshing(false);
                    }
                }, 1000);
            }
        });

        //加载  判断是都到列表底部
        rc_listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                isReflush = false;
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (rc_bean.isHasNextPage()) {
                            pullNum++;
                            presenter.getRecordData(String.valueOf(SharedHelper.get(RecordActivity.this, Api.USER_ID, Api.nul)), pullNum);
                        } else {
                            showToast("没有更多数据了");
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

    }

    //设置toolbar
    private void inSetToolBar() {
        back.setVisibility(View.VISIBLE);
        title.setText("交易记录");
    }


    //关闭当前
    @OnClick(R.id.toolbar_Back)
    void onBack() {
        finish();
    }


    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void getData(RecordBean bean) {
        rc_pull.setRefreshing(false);
        rc_bean = bean;
        if (isReflush) {
            beanList.clear();
        }
        if (bean.getList() != null && bean.getList().size() > 0) {
            beanList.addAll(bean.getList());
            adapter.notifyDataSetChanged();
            tvMessage.setVisibility(View.GONE);
        } else {
            tvMessage.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }
}
